package com.openbravo.pos.panels;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JPanel;

import com.openbravo.bean.crm.CustomerStoreValueCard;
import com.openbravo.beans.JPasswordDialog;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.editor.JEditorCurrencyPositive;
import com.openbravo.pos.base.AppLocal;
import com.openbravo.pos.util.Hashcypher;

import cn.ekuma.epos.datalogic.I_DataLogicSales;
import cn.ekuma.swing.dialog.I_EnterKeyProcesser;

public class JStoreCardValueOpPanel extends javax.swing.JPanel implements I_EnterKeyProcesser{
    
    private Double m_dPaid;

    private CustomerStoreValueCard storeCard;
    JStoreValueCardFinder cardFinder;
    
    /** Creates new form JPaymentDebt */
    public JStoreCardValueOpPanel(I_DataLogicSales dSales) {
    	setPreferredSize(new Dimension(568, 244));
        initComponents();  
        cardFinder=new JStoreValueCardFinder(dSales,m_jKeys);
        panel.add(cardFinder, java.awt.BorderLayout.CENTER);
    }
    
    public void activate() {
        cardFinder.reset();        
        m_jKeys.setEnabled(true);
        cardFinder.activate();  
    }
   
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel5 = new javax.swing.JPanel();
        jPanel6 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        m_jKeys = new com.openbravo.editor.JEditorKeys();
        jPanel3 = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        jPanel5.setLayout(new java.awt.BorderLayout());

        jPanel5.add(jPanel6, java.awt.BorderLayout.SOUTH);

        add(jPanel5, java.awt.BorderLayout.CENTER);
        
        panel = new JPanel();
        jPanel5.add(panel, BorderLayout.CENTER);
        panel.setLayout(new BorderLayout(0, 0));
        jPanel4 = new javax.swing.JPanel();
        jPanel4.setPreferredSize(new Dimension(10, 80));
        panel.add(jPanel4, BorderLayout.SOUTH);
        jLabel8 = new javax.swing.JLabel();
        m_jMoneyEuros = new JEditorCurrencyPositive();
        m_jMoneyEuros.addEditorKeys(m_jKeys);
                jPanel4.setLayout(null);
                
                        jLabel8.setText(AppLocal.getIntString("Label.InputCash")); // NOI18N
                        jPanel4.add(jLabel8);
                        jLabel8.setBounds(20, 5, 100, 15);

                                m_jMoneyEuros.setPreferredSize(new java.awt.Dimension(150, 25));
                                jPanel4.add(m_jMoneyEuros);
                                m_jMoneyEuros.setBounds(120, 0, 150, 25);

        jPanel2.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));
        jPanel1.add(m_jKeys);

        jPanel3.setBorder(javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5));
        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel1.add(jPanel3);

        jPanel2.add(jPanel1, java.awt.BorderLayout.NORTH);

        add(jPanel2, java.awt.BorderLayout.EAST);
    }// </editor-fold>//GEN-END:initComponents
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private com.openbravo.editor.JEditorKeys m_jKeys;
    private JEditorCurrencyPositive m_jMoneyEuros;
    private JPanel panel;
    // End of variables declaration//GEN-END:variables

	@Override
	public boolean enterKeyProc() {
		cardFinder.findStoreValueCardByCardNum();
		storeCard=cardFinder.getCard();
        if(storeCard!=null){
        	if(storeCard.getState()!=CustomerStoreValueCard.STATE_UseAble){
        		MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.storeCardNoUseAble"));
                msg.show(this);
                cardFinder.reset();
                return false;
        	}
				// comprobemos la clave antes de entrar...
                String sPassword = JPasswordDialog.showEditPassword(this,
                        AppLocal.getIntString("Label.Password"));
                if (sPassword != null&&Hashcypher.authenticate(storeCard.getPassword(),sPassword)) { 
                	m_dPaid=m_jMoneyEuros.getDoubleValue();     
                 } else {
                        MessageInf msg = new MessageInf(MessageInf.SGN_WARNING, AppLocal.getIntString("message.BadPassword"));
                        msg.show(this);
                        cardFinder.reset();
                        return false;
                 }
            }
	      return true;
	}

	public Double getM_dPaid() {
		return m_dPaid;
	}

	public CustomerStoreValueCard getStoreCard() {
		return storeCard;
	}
	
	public void reset(){
		storeCard=null;
		cardFinder.reset();
		m_dPaid=null;
	}
}
